package com.wtwd.device.service.impl;


import com.wtwd.device.service.GatewayRedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * @author mjy
 * @date 2021/06/08
 * @description
 */
@Service
public class GateWayRedisServiceImpl implements GatewayRedisService {

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public boolean hasKey(Object key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public String getValue(Object key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    public Map<Object, Object> getUserMsgByToken(String token) {
        // 拿到对应Token的key
        String userKey = (String) redisTemplate.opsForValue().get(token);
        if (StringUtils.isBlank(userKey)) {
            return new HashMap<>();
        }
        // 拿到这个key对应的用户数据
        Map<Object, Object> userMsg = redisTemplate.opsForHash().entries(userKey);
        return userMsg;
    }


}
